package com.controller;

/**
 * Created by head79 on 2017/7/1.
 */
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.commons.io.FilenameUtils;
import org.apache.ibatis.annotations.Param;
import org.json.JSONObject;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

/**
 * Ajax异步上传图片
 *
 * @author Hang.W
 * @version 1.0, 2017-02-14 00:51:39
 */
@Controller
public class UploadController {


    /**
     * 使用Ajax异步上传图片
     *
     * @param pic 封装图片对象
     * @param request
     * @param response
     * @throws IOException
     * @throws IllegalStateException
     */
    @RequestMapping(value = "/upload/uploadPic",method = RequestMethod.POST)
    public void uploadPic(@Param("pic") MultipartFile pic, HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException {

        try {
            // 获取图片原始文件名
            String originalFilename = pic.getOriginalFilename();
            System.out.println(originalFilename);
            // 文件名使用当前时间
            String name = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());

            // 获取上传图片的扩展名(jpg/png/...)
            String extension = FilenameUtils.getExtension(originalFilename);

            // 图片上传的相对路径（因为相对路径放到页面上就可以显示图片）
            String path ="/upload/" + name + "." + extension;
            // 图片上传的绝对路径
            String url = request.getSession().getServletContext().getRealPath("") + path;
            File dir = new File(url);
            if(!dir.exists()) {
                dir.mkdirs();
            }
            // 上传图片
            pic.transferTo(new File(url));
            // 将相对路径写回（json格式）
            JSONObject jsonObject = new JSONObject();
            // 将图片上传到本地
            jsonObject.put("path", path);

            // 设置响应数据的类型json
            response.setContentType("application/json; charset=utf-8");
            // 写回
            response.getWriter().write(jsonObject.toString());

        } catch (Exception e) {
            throw new RuntimeException("服务器繁忙，上传图片失败");
        }
    }
    @Test
    public void test(){
        for (int i=0;i<100;i++){
            int ii = (int) Math.random()*100;
            System.out.println(i);
        }
    }
    @Test
    public void test1(){
        String str ="abcss";
        String string[] = str.split("");
        StringBuffer sb = new StringBuffer();
        for (int i=string.length-1;i>=0;i--){
            sb.append(string[i]);
        }
        new StringBuilder(str).reverse().toString();
        new StringBuffer(str).reverse().toString();
        System.out.println(new StringBuffer(str).reverse().toString());
    }

}